<?php
/**
 + Created by:	Webit Solution
 * Company:		Webit Solution
 + Contact:		www.webitsolution.com , info@webitsolution.com
				www.wrecruit.com, developer@wrecruit.com
 * Created on:	Dec 19, 2012
 ^
 + Project: 		WRecruit 
 * File Name:	Pplugin/wrecruitregister.php
 ^ 
 * Description: Plugin for WRecruit
 ^ 
 * History:		NONE
 ^ 
 */

defined( '_JEXEC' ) or die( 'Restricted access' );

class plgSystemWRecruitRegister extends JPlugin
{
		function onAfterStoreUser($user, $isnew, $success, $msg)
		{
			if( $isnew )
			{
				$db = &JFactory::getDBO();
				$created = date('Y-m-d H:i:s');
				$query = "INSERT INTO #__wrecruit_userroles (uid,role,dated) VALUES (".$user['id'].", ".$user['userrole'].", '".$created."')";
				$db->setQuery( $query );
				$db->query();
			}
		}

	   function onUserAfterDelete( $user, $success, $msg )
		{
			$db = &JFactory::getDBO();
			$query = 'DELETE FROM #__wrecruit_userroles WHERE uid ='.$user['id'];
			$db->setQuery( $query );
			$db->query();
			return true;
		}

		function onAfterDispatch()
        {
			$document = &JFactory::getDocument();
			$content = $document->getBuffer('component');
			$option = JRequest::getVar('option');
			$view = JRequest::getVar('view');
			$html = $this->getRoleHTML();
			$lang = & JFactory :: getLanguage();
			$lang->load('plg_content_wrecruitregister', JPATH_ADMINISTRATOR);

			$newcontent = "";
			if($option == 'com_user' || $option == 'com_users'){
				if($view == 'register' || $view == 'registration')	$checkcontent = '<button class="button validate" type="submit">'.JTEXT::_('REGISTER').'</button>';
				
				$newcontent = str_replace($checkcontent,$html.$checkcontent,$content);
			}
			if($newcontent!="")	{
				$document->setBuffer($newcontent,'component');
			}
        }
		function getRoleHTML()
		{
			jimport( 'joomla.html.parameter' );
			$plugin 	=& JPluginHelper::getPlugin('system', 'wrecruitregister');
			$this->params   	= new JParameter($plugin->params);
			JPlugin::loadLanguage( 'plg_system_wrecruitregister', JPATH_ADMINISTRATOR );
			
			$userregisterinrole = $this->params->get('userregisterinrole');
			
			$jsrole = JRequest::getVar('jsrole');
			if($userregisterinrole == 2) $jsrole = 1; // enforce employer
			elseif($userregisterinrole == 3) $jsrole = 2; // enforce employer
			
			if($jsrole){
				if ($jsrole == 1){ // employer
					$rolehtml = "<input type='hidden' name='userrole' value='1'>".JText::_('JS_EMPLOYER');
				}else $rolehtml = "<input type='hidden' name='userrole' value='2'>".JText::_('JS_JOBSEEKER');
				
				$returnvalue = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
								  <tr><td width=\"120\"  >Role:</td><td >".$rolehtml."</td></tr></table>";
			}else{
				$rolehtml = "<select name='userrole'>
							<option value='1'>".JText::_('JS_EMPLOYER')."</option>
							<option value='2'>".JText::_('JS_JOBSEEKER')."</option>
						</select>";
				$returnvalue = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
								  <tr><td width=\"120\"  >Role:</td><td >".$rolehtml."</td></tr></table>";
			}					
			return $returnvalue;				
		}
		
}
?>
